#include "../include/TaskQueue.hpp"


wd::TaskQueue::TaskQueue(int queSize)
:_que()
,_queSize(queSize)
,_mutex()
,_notFull(_mutex)
,_notEmpty(_mutex)
,_flag(true)
{}

void wd::TaskQueue::push(ElemType e){
    _mutex.lock();
    
    while(full()){
        _notFull.wait();
    }

    _que.push(e);
    _notEmpty.notifyOne();
    _mutex.unlock();


}

ElemType wd::TaskQueue::pop(){

    _mutex.lock();
    
    while(empty() && _flag){
        _notEmpty.wait();
    }

    ElemType ret = _que.front();
    _que.pop();

    _notFull.notifyOne();
    _mutex.unlock();
    return ret;
}

bool wd::TaskQueue::full(){
    return _que.size() == (unsigned long) _queSize;
}

bool wd::TaskQueue::empty(){
    return _que.size() == 0;
}

void wd::TaskQueue::wakeup(){
    _flag = false;
    _notEmpty.notifyAll();
}
